package com.xinjie.www.servlet;

import com.xinjie.www.po.User;
import com.xinjie.www.service.report.ReportService;
import com.xinjie.www.service.report.ReportServiceImpl;
import com.xinjie.www.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class ReportServlet extends HttpServlet {
    ReportService reportService = new ReportServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("do".equals(method)){
            this.doReport(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void doReport(HttpServletRequest req, HttpServletResponse resp) {
        int postId = Integer.parseInt(req.getParameter("postId"));
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        User user = (User) o;
        try {
            boolean b = reportService.doReport(postId, user.getUserId());
            if (b){
                req.getSession().setAttribute("doReportSuccess","举报成功，请等待处理");
                resp.sendRedirect("/UserPage/PostList.jsp");
            }else {
                req.getSession().setAttribute("doReportLose","举办失败");
                resp.sendRedirect("/UserPage/PostList.jsp");
            }
        } catch (SQLException | ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
    }
}
